The Python subprocess module supports running programs from within Python. It has a variety of functions for running programs in different ways, as described in the Python subprocess module documentation. The shlex module is a useful companion for splitting up command lines to use with the subprocess module.


In [15]:
import os
print os.environ['PATH']


/home/pvh/anaconda/bin:/home/pvh/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/pvh/Documents/eclipse_cl/bin/x86_64_linux:/home/pvh/Documents/pycharm-community-3.1.1/bin:/usr/local/cuda-6.0/bin:/home/pvh/.rvm/bin:/home/pvh/.rvm/bin

In [11]:
import subprocess
import shlex
import sys
import os

print "working directory:", os.getcwd()
cmd_str = 'echo "Hello   World"'
cmd = shlex.split(cmd_str)
print cmd
subprocess.check_output(cmd)


working directory: /home/pvh/Documents/python
['echo', 'Hello   World']
Out[11]:
'Hello   World\n'

In [12]:
subprocess.call(['echo', 'Hello World'])


Out[12]:
0

In [21]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
print output_file
with output_file:
    exit_code = subprocess.call(['grep','pvh','/etc/passwd'], stdout=output_file)
print "Done", exit_code


<open file '/home/pvh/Documents/python/grep_output.txt', mode 'w' at 0x7f2534347930>
Done

In [41]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
err_out_file = open('/home/pvh/Documents/python/grep_error.txt', 'w')
exit_code = subprocess.call(['grep','pvh','/etc/passssswd'], 
                            stdout=output_file, 
                            stderr=err_out_file)
output_file.close()
err_out_file.close()
print "Done", exit_code


Done 2

In [ ]:
output_file = open('/home/pvh/Documents/python/grep_output.txt', 'w')
err_out_file = open('/home/pvh/Documents/python/grep_error.txt', 'w')
exit_code = subprocess.call(['grep','pvh','/etc/passssswd'], 
                            stdout=output_file, 
                            stderr=subprocess.STDOUT)
output_file.close()
err_out_file.close()
print "Done", exit_code

In [17]:
subprocess.call(['/opt/Zotero_linux-x86_64/zotero'])
print "Hello Everyone"


Hello Everyone

In [26]:
input_file = open('python.fasta')
output_file = open('one_orchid.txt', 'w')
cmd_str = 'grep "P.villosum"'
cmd = shlex.split(cmd_str)
subprocess.call(cmd, stdin=input_file, stdout=output_file)


Out[26]:
0

In [36]:
input_file = open('python.fasta')
cmd_str = 'grep "P.villosum"'
cmd = shlex.split(cmd_str)
results = subprocess.check_output(cmd, stdin=input_file)
print "I found P.villosum in {}".format(results.split()[0][1:])


I found P.villosum in gi|2765608|emb|Z78483.1|PVZ78483

In [38]:
cmd_str = 'grep "P.villosum" /etc/fishfishfish'
cmd = shlex.split(cmd_str)
try:
    results = subprocess.check_output(cmd)
except subprocess.CalledProcessError:
    print >>sys.stderr,"Running the command ({}) failed".format(cmd_str)
else:
    print "I got a result:", results


Running the command (grep "P.villosum" /etc/fishfishfish) failed

In [44]:
name = "Peter"
cmd = "echo Hello {}".format(name)
subprocess.check_output(cmd, shell=True)


Out[44]:
'Hello Peter\n'

In [45]:
name = "Peter ; echo And now for something evil"
cmd = "echo Hello {}".format(name)
subprocess.check_output(cmd, shell=True)


Out[45]:
'Hello Peter\nAnd now for something evil\n'

In [51]:
proc1 = subprocess.Popen(['/opt/Zotero_linux-x86_64/zotero'])
proc2 = subprocess.Popen(['/usr/bin/gedit'])
proc1.wait()
proc2.wait()


Out[51]:
0

In [50]:
subprocess.call(['/opt/Zotero_linux-x86_64/zotero'])
subprocess.call(['/usr/bin/gedit'])


Out[50]:
0

In [ ]:
proc1 = subprocess.Popen(['/opt/Zotero_linux-x86_64/zotero'])
proc2 = subprocess.Popen(['/usr/bin/gedit'])
proc1.wait()
proc2.wait()

In [56]:
cmd_str = 'grep "P.villosum" python.fasta'
cmd = shlex.split(cmd_str)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
(proc_output, proc_error) = proc.communicate()
exit_code = proc.wait()
print "output:", proc_output
print "error:", proc_error
print "exit code:", exit_code


output: >gi|2765608|emb|Z78483.1|PVZ78483 P.villosum 5.8S rRNA gene and ITS1 and ITS2 DNA

error: 
exit code: 0

In [57]:
cmd_str = 'grep "P.villosum" python.fasta'
cmd = shlex.split(cmd_str)
proc = subprocess.Popen(cmd)
(proc_output, proc_error) = proc.communicate()
exit_code = proc.wait()
print "output:", proc_output
print "error:", proc_error
print "exit code:", exit_code


output: None
error: None
exit code: 0

In [59]:
cmd_str = "grep chicken"
cmd = shlex.split(cmd)
data = """chicken
fish
pork
chicken
snake
aardvark"""
#data = "chicken\nfish"
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE)
(proc_output, proc_error) = proc.communicate(data)
print "output:", proc_output
print "error:", proc_error
exit_code = proc.wait()
print "exit code:", exit_code


output: chicken
chicken

error: None
exit code: 0

In [60]:
cmd_str = 'sleep 60'
cmd = shlex.split(cmd_str)
count = 4
procs = []
for i in range(count):
    procs.append(subprocess.Popen(cmd))

for proc in procs:
    proc.wait()

In [ ]: